package com.apache.common.plugins;

import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * @Classname SelectActAndGiveListPluginImpl
 * @Description 查询指定人拥有的所有权限（来源于角色权限或者直接分配的权限）
 * @Date 2020/6/16 22:57
 * @Created by fpc
 */
public class SelectActAndGiveListPluginImpl implements CustomMethodPlugin {
    protected Logger log = LoggerFactory.getLogger(getClass());
    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        String delStatus = String.valueOf(map.get("delStatus"));//指定的用户id
        if("1".equals(delStatus)){
            return null;
        }
        String fatherId = StrUtil.doNull(String.valueOf(map.get("uctAct.w_fatherId")),"0");//父级权限id
        String userId = String.valueOf(map.get("userId"));//指定的用户id
        String sysEname = String.valueOf(map.get("uctAct.w_sysEname"));//指定的用户id
        if(StrUtil.isNull(userId)){
            map.put(CustomMethodPlugin.resultMark,"T");
            map.put(CustomMethodPlugin.pluginInnerReturn,"必要参数不能为空");
            return null;
        }
        StringBuffer sb = new StringBuffer();
        String columns=" DISTINCT uctAct.act_id AS 'uctAct@act_id',uctAct.act_cname AS 'uctAct@act_cname',uctAct.father_id AS 'uctAct@father_id',uctAct.full_ename AS 'uctAct@full_ename',uctAct.sub_count AS 'uctAct@sub_count',uctAct.full_cname AS 'uctAct@full_cname' ";
        sb.append("SELECT ");
        sb.append(columns);
        sb.append(" FROM uct_act uctAct,uct_act_give uctActGive WHERE uctAct.act_id = uctActGive.act_id ");
        sb.append(" AND uctAct.father_id='"+fatherId+"'");
        sb.append(" AND uctAct.sys_ename='"+sysEname+"'");
        sb.append(" AND(  ");
        sb.append(" (uctActGive.role_id = '"+userId+"' AND uctActGive.give_type = 'user') ");
        sb.append(" OR (uctActGive.role_id in ( SELECT role_id FROM uct_role_give uctRoleGive WHERE uctRoleGive.user_id = '"+userId+"') AND uctActGive.give_type = 'role')");
        sb.append(" )");
        log.info("sql："+sb.toString());
        List actList = (List)IusPluginUtil.getInstance().select("plateform", sb.toString(), 0, 0);
        map.put(CustomMethodPlugin.resultMark,"T");
        map.put(CustomMethodPlugin.pluginInnerReturn,actList);
        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }
}
